package com.ugrow.ugrowcommunity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.teacher_app.TeacherMainActivity;
import com.example.teacher_app.bean.TeacherBean;
import com.example.teacher_app.data.TeacherData;
import com.example.teacher_app.tch_activity.tch_campus.CampusCardActivity;
import com.google.gson.Gson;
import com.ugrow.ugrowcommunity.activity.web.WebActivity;
import com.ugrow.ugrowcommunity.bean.StudentBean;
import com.ugrow.ugrowcommunity.data.Data;
import com.ugrow.ugrowcommunity.databinding.ActivityLoginBinding;
import com.ugrow.ugrowcommunity.dialog.LoadProgressDialog;
import com.ugrow.ugrowcommunity.util.HttpUtil;

import java.io.IOException;
import java.util.HashMap;


public class LoginActivity extends AppCompatActivity {

    private ActivityLoginBinding binding;
    private String loginType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityLoginBinding.inflate(getLayoutInflater());
        setTheme(R.style.WhiteTheme);
        setContentView(binding.getRoot());
        binding.login.setOnClickListener(  v -> login());
        binding.user1.setHintTextAppearance(R.style.hintStyle);
        binding.pwd1.setHintTextAppearance(R.style.hintStyle);

        binding.rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.student:
                        loginType = "student";
                        break;
                    case R.id.teacher_name:
                        loginType = "teacher";
                        break;
                }
            }
        });
        binding.student.setChecked(true);

    }

    private void login() {
        if (loginType == null || loginType.isEmpty()) {
            Toast.makeText(this, "请先选择登录类型", Toast.LENGTH_SHORT).show();
            return;
        }
        if (binding.user.getText().toString().isEmpty() && binding.pwd.getText().toString().isEmpty()) {
            Toast.makeText(this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if (binding.student.isChecked()) {
            LoadProgressDialog loadProgressDialog = new LoadProgressDialog(this, "请稍等...", false);
            loadProgressDialog.show();
            HashMap<String, Object> map = new HashMap<>();
            map.put("stuNum", binding.user.getText().toString());
            map.put("stuPassword", binding.pwd.getText().toString());
            HttpUtil.httpPost(BuildConfig.STUDENT_LOGIN, map, HttpUtil.ContentType.JSON, new HttpUtil.CallBack() {
                @Override
                public void success(HttpUtil.Result result) {
                    loadProgressDialog.dismiss();
                    if (result.code == 200){
                        Data.StudentBean = new Gson().fromJson(result.content, StudentBean.class);
                        Data.ID = Data.StudentBean.getData().get(0).getId();
                        Data.StuNum = Data.StudentBean.getData().get(0).getStuNum();
                        startActivity(new Intent(LoginActivity.this, MainActivity.class));
                        finish();
                    }
                    else {
                        StudentBean studentBean = new Gson().fromJson(result.content, StudentBean.class);
                        Toast.makeText(LoginActivity.this,studentBean.getMessage() , Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void failed(IOException e) {
                    loadProgressDialog.dismiss();
                    Toast.makeText(LoginActivity.this, "网络连接异常！", Toast.LENGTH_SHORT).show();
                }
            });

        } else if (binding.teacher.isChecked()) {
            LoadProgressDialog loadProgressDialog = new LoadProgressDialog(this, "请稍等...", false);
            loadProgressDialog.show();
            HashMap<String, Object> map = new HashMap<>();
            map.put("tchNum", Integer.parseInt(binding.user.getText().toString()));
            map.put("tchPassword", binding.pwd.getText().toString());
            HttpUtil.httpPost(BuildConfig.TEACHER_LOGIN, map, HttpUtil.ContentType.JSON, new HttpUtil.CallBack() {
                @Override
                public void success(HttpUtil.Result result) {
                    if (result.code == 200){
                        loadProgressDialog.dismiss();
                        TeacherData.TeacherBean= new Gson().fromJson(result.content, TeacherBean.class);
                        TeacherData.ID = TeacherData.TeacherBean.getData().get(0).getTchId();
                        TeacherData.TchNum = TeacherData.TeacherBean.getData().get(0).getTchNum();
                        startActivity(new Intent(LoginActivity.this, TeacherMainActivity.class));
                        finish();
                    }else {
                        loadProgressDialog.dismiss();
                        TeacherBean teacherBean= new Gson().fromJson(result.content, TeacherBean.class);
                        Toast.makeText(LoginActivity.this,teacherBean.getMessage() , Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void failed(IOException e) {
                    loadProgressDialog.dismiss();
                    Toast.makeText(LoginActivity.this, "网络连接异常！", Toast.LENGTH_SHORT).show();
                }
            });
        }

    }
}